def update_pheno_table(dataset, dbname, shard, test):
    usr = os.popen('whoami').read().strip()
    from utils import get_conf, pg_url
    conf = get_conf()
    dbuser = conf["dbuser"]
    password = conf["password"]
    portnum = conf["portnum"]
    dbstring_usr = "%s://%s@:%s/postgres"%(pg_url(), usr, portnum)
    dbstring = "%s://%s:%s@localhost:%s/postgres"%(pg_url(), dbuser, password, portnum)
    dbstring_dbname = "%s://%s:%s@localhost:%s/%s"%(pg_url(), dbuser, password, portnum, dbname)
    from utils import create_engine_wrapper
    from dbutils import dbuser_exists, db_exists, schema_exists
    db_usr = create_engine_wrapper(dbstring_usr)
    db = create_engine_wrapper(dbstring)
    db_dbname = create_engine_wrapper(dbstring_dbname)
    usr = os.popen('whoami').read().strip()
    if not dbuser_exists(db_usr, dbuser):
        sys.exit("dbuser '%s' does not exist\nrun the init_db.py script, exiting"%dbuser)
    if not db_exists(db, dbname):
        sys.exit("database '%s' does not exist\nrun the init_db.py script, exiting"%dbname)

    from dbutils import get_db_type
    dbtype = get_db_type(dbstring_dbname)
    dirpath = os.path.dirname(os.path.abspath(__file__))
    datasets = conf[dbtype].sections
    if dataset not in datasets:
        sys.exit("error, dataset %s is not listed in the config as one of the datasets associated with database type %s"%(dataset, dbtype))

    # Choose schema depending on shard and test options.
    if shard and test:
        schema = dataset + "_shard_test"
    elif shard:
        schema = dataset + "_shard"
    elif test:
        schema = dataset + "_test"
    else:
        schema = dataset

    if not schema_exists(db_dbname, schema):
        sys.exit("schema '%s' does not exist, exiting"%schema)

    print "using schema %s in database %s"%(schema, dbname)
    phenofiles = conf[dbtype][dataset]["phenofiles"]
    from pheno import Pheno_Base
    from dbschema import make_pheno_all
    table_dict = make_pheno_all(schema)
    Pheno_Base(phenofiles, dbstring_dbname, table_dict).update_pheno_table()

if __name__=='__main__':
    import sys, os
    from optparse import OptionParser
    usage = "'%prog dataset db'."
    parser = OptionParser(usage=usage)
    parser.add_option("-s", "--sharding", action="store_true", dest="shard", help="use geno table sharding")
    parser.add_option("-t", "--test", action="store_true", dest="test", default=False, help="test cases")
    (options, args) = parser.parse_args()

    if len(args) != 2:
        print "incorrect number of arguments"
        parser.print_help()
        sys.exit(1)

    dataset = args[0]
    dbname = args[1]

    update_pheno_table(dataset, dbname, options.shard, options.test)
